package com.sc.web.gwt.client.view.administratorwidget.contents;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.sc.web.gwt.client.service.AgentAdminDataManagementService;
import com.sc.web.gwt.client.service.AgentAdminDataManagementServiceAsync;
import com.sc.web.gwt.client.view.uicomponent.Dialog;
import com.sc.web.gwt.client.view.uicomponent.ValidatablePasswordTextBox;
import com.sc.web.gwt.client.view.uicomponent.ValidatableTextBox;
import com.sc.web.gwt.shared.model.AgentAdmin;

public class AddOrEditAgentAdminWidget extends Dialog {
    private static Binder binder = GWT.create(Binder.class);
    final Button confirmBtn = new Button("OK");
    final Button cancelBtn = new Button("Cancel");

    AgentAdminDataManagementServiceAsync agentAdminDataManagementServiceAsync = GWT.create(AgentAdminDataManagementService.class);

    private AgentAdmin agentAdmin;

    @UiField
    ValidatableTextBox userName;
    @UiField
    ListBox state;
    @UiField
    ValidatableTextBox email;
    @UiField
    Label popupTitle;
    @UiField
    ValidatableTextBox siteName;
    @UiField
    ValidatableTextBox company;
    @UiField
    ValidatableTextBox fullName;
    @UiField
    ValidatableTextBox phoneNo;
    @UiField
    ValidatableTextBox position;
    @UiField
    ValidatablePasswordTextBox password;

    public interface Binder extends UiBinder<HTMLPanel, AddOrEditAgentAdminWidget> {

    }

    public AddOrEditAgentAdminWidget(AgentAdmin agent, ViewAllAgentsWidget parentWidget) {
        final ViewAllAgentsWidget thisParentWidget = parentWidget;
        final AddOrEditAgentAdminWidget thisWidget = this;
        setBody(binder.createAndBindUi(this));
        confirmBtn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                agentAdmin.setAgentAdminPhoneNumber(phoneNo.getText());
                agentAdmin.setAgentAdminPosition(position.getText());
                agentAdmin.setAgentAdminAccount(userName.getText());
                agentAdmin.setSiteName(siteName.getText());
                agentAdmin.setAgentAdminName(fullName.getText());
                agentAdmin.setAgentAdminEmail(email.getText());
                agentAdmin.setAgentAdminCompany(company.getText());
                agentAdmin.setState(state.getValue(state.getSelectedIndex()));
                if (agentAdmin.getAgentAdminPassword() == null) {
                    agentAdmin.setAgentAdminPassword(password.getText());
                }
                agentAdminDataManagementServiceAsync.saveOrUpdateAgentAdminData(agentAdmin, new AsyncCallback<Boolean>() {
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                    }

                    public void onSuccess(Boolean result) {
                        if (result) {
                            thisParentWidget.refreshTable();
                            thisWidget.hide();
                        } else Window.alert("Cannot save agent. Please re-check the information you have provided.");
                    }
                });
            }
        });

        cancelBtn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                thisWidget.hide();
            }
        });

        addButton(confirmBtn);
        addButton(cancelBtn);
        state.addItem("Valid");
        state.addItem("Suspended");
        state.setSelectedIndex(0);

        agentAdmin = agent;
        if (agentAdmin.getAgentAdminId() == -1) {
            userName.setEnabled(true);
            popupTitle.setText("Add new agent");
            if (agentAdmin.getAgentAdminPassword() != null) password.setText(agentAdmin.getAgentAdminPassword());
        } else {
            userName.setEnabled(false);
            password.setEnabled(false);
            password.setText("********");
            popupTitle.setText("Edit agent");
        }
        if (agentAdmin.getAgentAdminAccount() != null) userName.setText(agentAdmin.getAgentAdminAccount());
        if (agentAdmin.getState() != null) {
            state.setSelectedIndex(agentAdmin.getStateId() - 1);
        }
        if (agentAdmin.getAgentAdminEmail() != null) email.setText(agentAdmin.getAgentAdminEmail());
        if (agentAdmin.getSiteName() != null) {
            siteName.setText(agentAdmin.getSiteName());
            siteName.setEnabled(false);
        }
        if (agentAdmin.getAgentAdminCompany() != null) company.setText(agentAdmin.getAgentAdminCompany());
        if (agentAdmin.getAgentAdminName() != null) fullName.setText(agentAdmin.getAgentAdminName());
        if (agentAdmin.getAgentAdminPhoneNumber() != null) phoneNo.setText(agentAdmin.getAgentAdminPhoneNumber());
        if (agentAdmin.getAgentAdminPosition() != null) position.setText(agentAdmin.getAgentAdminPosition());
    }

    public void showWidget() {
        center();
        show();
    }
}